package it.unisannio.pgridporting;

import it.unisannio.pgridporting.Contact;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.StringTokenizer;

import android.content.ContentUris;
import java.util.Vector;
import com.google.wireless.gdata.contacts.data.ContactEntry;
import com.google.wireless.gdata.contacts.data.ContactsFeed;

import android.app.Activity;
import android.app.ListActivity;
import android.app.NotificationManager;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.provider.Contacts;
import android.provider.Contacts.ContactMethods;
import android.provider.Contacts.ContactMethodsColumns;
import android.provider.Contacts.People;
import android.provider.Im.ContactList;
import android.provider.Im.ContactListColumns;
import android.provider.Im.ContactsColumns;
import android.server.BluetoothService;
import android.syncml.pim.vcard.ContactStruct;
import android.syncml.pim.vcard.ContactStruct.ContactMethod;
import android.view.Menu;
import android.view.View;
import android.view.Menu.Item;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import org.bluez.Adapter;
import org.bluez.Manager;
import android.util.Log;
import org.bluez.IBluetoothService;
import org.bluez.BluetoothServiceNative;

import p2p.basic.P2P;
import p2p.basic.P2PFactory;
import p2p.basic.Peer;
import p2p.storage.DataItem;
import p2p.storage.Storage;
import p2p.storage.StorageFactory;
import p2p.storage.Type;
import p2p.storage.TypeHandler;
import pgrid.Constants;
import pgrid.interfaces.basic.PGridP2P;
import pgrid.interfaces.basic.PGridP2PFactory;
import pgrid.interfaces.storage.PGridStorageFactory;
import test.SimpleTypeHandler;
import android.content.Context;
import android.os.Handler;
import android.database.Cursor;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.view.Menu;
import android.view.View;
import android.view.Menu.Item;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import android.provider.Im;
import android.app.AlertDialog;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.content.DialogInterface;

public class AddContact extends Activity {

	/** Called when the activity is first created. */
	final static String LOG_TAG = "myTag";

	public static final int CONTATTI = Menu.FIRST - 1;
	public static final int RICERCA = Menu.FIRST;
	public static final int INSERIMENTO = Menu.FIRST + 1;
	public static final int DELETE = Menu.FIRST + 2;

	// Variabili globali per gestione bottoni
	String name, query;
	String idvalue1, idvalue2, idvalue3, idvalue4, idvalue5, idvalue6,
			idvalue7, idvalue8, idvalue9, idvalue10;
	Cursor cur, conta;
	int id;
	Context db_Context;

	String idvalue;

	private static final String DATABASE_CREATE = "CREATE TABLE IF NOT EXISTS Utenti_Shared(_id integer primary key autoincrement, "
			+ "id_utente text not null," + "dati text not null" + ");";

	private static final String DATABASE_NAME = "PgridPorting";

	private static final String DATABASE_TABLE = "Utenti_Shared";

	private static final int DATABASE_VERSION = 1;

	private SQLiteDatabase db;

	private TextView tv;

	private Uri id_eliminare_1, id_eliminare_2, id_eliminare_3, id_eliminare_4,
			id_eliminare_5, id_eliminare_6, id_eliminare_7;
	private Uri id_eliminare_8, id_eliminare_9, id_eliminare_10;

	private PGridManager pGridManager;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		File dir = this.getDir("myDir", Activity.MODE_PRIVATE);
		File dataDir = this.getDir("data", Activity.MODE_PRIVATE);

		try {
			Constants.DATA_DIR = dataDir.getCanonicalPath()
					+ System.getProperty("file.separator");
			Log.v("MyMessage", "DATA_DIR defined \n");
		} catch (IOException e3) {
			Log.v("MyMessage", "DATA_DIR defined error");
			e3.printStackTrace();
		}

		String dirPath = null;
		try {
			dirPath = dir.getCanonicalPath();
		} catch (IOException e1) {
			Log.v("MyMessage", "Problems in dir creation");
			e1.printStackTrace();
		}

		/**
		 * Init P-Grid
		 */

		pGridManager = new PGridManager(dirPath);

		// CONTACT ADDER
		setContentView(R.layout.main);

		db_Context = this;

		TabHost tabs = (TabHost) this.findViewById(R.id.tabs);
		tabs.setup();

		TabSpec one = tabs.newTabSpec("one");

		one.setContent(R.id.titolo);
		one.setIndicator("PGridPorting", this.getResources().getDrawable(
				R.drawable.person));

		tabs.addTab(one);

		tabs.setCurrentTab(R.id.titolo);

		// Inserimento di alcuni Contatti NB I contatti delle due agende sono
		// diversi

		// creazioneAgenda1();

		// creazioneAgenda2();

	}

	// cancella tutti i contatti
	private void delete() {
		getContentResolver().delete(
				android.provider.Contacts.People.CONTENT_URI, null, null);
		Cursor c = managedQuery(android.provider.Contacts.People.CONTENT_URI, // URI
				null, null, // Clause
				null);
		AllContatti(c);
	}

	// cancella solo un contatto
	private void getCancella(String query) {
		getContentResolver().delete(
				android.provider.Contacts.People.CONTENT_URI, "_id=" + query,
				null);
		Cursor c = managedQuery(android.provider.Contacts.People.CONTENT_URI,
				null, // Which
				null, null);
		c.commitUpdates();

	}

	// Ricerca tutti i contatti presenti in rubrica
	private void all() {
		Cursor c = managedQuery(android.provider.Contacts.People.CONTENT_URI,
				null, // Which
				null, android.provider.Contacts.People.NAME + " ASC");
		AllContatti(c);

	}

	/**
	 * @param c
	 */
	public void AllContatti(Cursor c) {
		setContentView(R.layout.contatti);
		String name;
		int nameColumn;

		EditText contatto1 = (EditText) findViewById(R.id.contatto1);
		EditText contatto2 = (EditText) findViewById(R.id.contatto2);
		EditText contatto3 = (EditText) findViewById(R.id.contatto3);
		EditText contatto4 = (EditText) findViewById(R.id.contatto4);
		EditText contatto5 = (EditText) findViewById(R.id.contatto5);
		EditText contatto6 = (EditText) findViewById(R.id.contatto6);
		EditText contatto7 = (EditText) findViewById(R.id.contatto7);
		EditText contatto8 = (EditText) findViewById(R.id.contatto8);
		EditText contatto9 = (EditText) findViewById(R.id.contatto9);
		EditText contatto10 = (EditText) findViewById(R.id.contatto10);

		Button cmd = null;

		ImageButton cmd1 = null;
		switch (c.count()) {
		case 0:
			break;

		case 1:
			c.first();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			id_eliminare_1 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			idvalue1 = name;
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto1.setText(name);

			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_1);

			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {

					// Inserire conferma cancellazione singolo contatto
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue1);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);

				}
			});

			break;

		case 2:
			c.first();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue1 = name;
			id_eliminare_1 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto1.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_1);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue1);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);

				}
			});

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue2 = name;
			id_eliminare_2 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto2.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_2);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue2);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();

			break;

		case 3:
			c.first();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue1 = name;
			id_eliminare_1 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto1.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_1);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue1);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);

				}
			});

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue2 = name;
			id_eliminare_2 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto2.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_2);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue2);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);

				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue3 = name;
			id_eliminare_3 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto3.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_3);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue3);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);

				}
			});

			c.commitUpdates();

			break;

		case 4:
			c.first();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue1 = name;
			id_eliminare_1 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto1.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_1);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue1);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);

				}
			});

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue2 = name;
			id_eliminare_2 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto2.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_2);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue2);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);

				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue3 = name;
			id_eliminare_3 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto3.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_3);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue3);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);

				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue4 = name;
			id_eliminare_4 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto4.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_4);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue4);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);

				}
			});

			c.commitUpdates();

			break;

		case 5:
			c.first();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue1 = name;
			id_eliminare_1 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto1.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_1);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue1);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);

				}
			});

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue2 = name;
			id_eliminare_2 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto2.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_2);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue2);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);

				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue3 = name;
			id_eliminare_3 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto3.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_3);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue3);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue4 = name;
			id_eliminare_4 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto4.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_4);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue4);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);

				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue5 = name;
			id_eliminare_5 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto5.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_5);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue5);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);

				}
			});

			c.commitUpdates();
			break;

		case 6:
			c.first();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue1 = name;
			id_eliminare_1 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto1.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_1);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue1);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);

				}
			});

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue2 = name;
			id_eliminare_2 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto2.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_2);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue2);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue3 = name;
			id_eliminare_3 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto3.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_3);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					getCancella(idvalue3);
					all();
				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue4 = name;
			id_eliminare_4 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto4.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_4);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					getCancella(idvalue4);
					all();
				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue5 = name;
			id_eliminare_5 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto5.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_5);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue5);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
			}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue6 = name;
			id_eliminare_6 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto6.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_6);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue6);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);

				}
			});

			c.commitUpdates();
			break;

		case 7:
			c.first();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue1 = name;
			id_eliminare_1 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto1.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_1);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue1);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);

				}
			});

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue2 = name;
			id_eliminare_2 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto2.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_2);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue2);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue3 = name;
			id_eliminare_3 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto3.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_3);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue3);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue4 = name;
			id_eliminare_4 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto4.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_4);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue4);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
			}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue5 = name;
			id_eliminare_5 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto5.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_5);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue5);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);

				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue6 = name;
			id_eliminare_6 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto6.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_6);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue6);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);

				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue7 = name;
			id_eliminare_7 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto7.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_7);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue7);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();

			break;

		case 8:
			c.first();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue1 = name;
			id_eliminare_1 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto1.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_1);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue1);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);


				}
			});

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue2 = name;
			id_eliminare_2 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto2.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_2);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue2);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue3 = name;
			id_eliminare_3 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto3.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_3);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					getCancella(idvalue3);
					all();
				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue4 = name;
			id_eliminare_4 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto4.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_4);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue4);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);

				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue5 = name;
			id_eliminare_5 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto5.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_5);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue5);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
			}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue6 = name;
			id_eliminare_6 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto6.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_6);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue6);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue7 = name;
			id_eliminare_7 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto7.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_7);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue7);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();
			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue8 = name;
			id_eliminare_8 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto8.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_8);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue8);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();

			break;
		case 9:
			c.first();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue1 = name;
			id_eliminare_1 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto1.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_1);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue1);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);

				}
			});

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue2 = name;
			id_eliminare_2 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto2.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_2);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue2);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue3 = name;
			id_eliminare_3 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto3.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_3);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue3);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue4 = name;
			id_eliminare_4 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto4.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_4);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue4);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue5 = name;
			id_eliminare_5 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto5.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_5);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue5);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue6 = name;
			id_eliminare_6 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto6.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_6);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue6);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue7 = name;
			id_eliminare_7 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto7.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_7);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue7);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();
			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue8 = name;
			id_eliminare_8 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto8.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_8);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue8);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();
			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue9 = name;
			id_eliminare_9 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto9.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_9);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue9);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();

			break;
		default:
			c.first();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue1 = name;
			id_eliminare_1 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto1.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_1);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue1);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);

				}
			});

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue2 = name;
			id_eliminare_2 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto2.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_2);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue2);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue3 = name;
			id_eliminare_3 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto3.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_3);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue3);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue4 = name;
			id_eliminare_4 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto4.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_4);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue4);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue5 = name;
			id_eliminare_5 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto5.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_5);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue5);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue6 = name;
			id_eliminare_6 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto6.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_6);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue6);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue7 = name;
			id_eliminare_7 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto7.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_7);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue7);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();
			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue8 = name;
			id_eliminare_8 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto8.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_8);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue8);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();
			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue9 = name;
			id_eliminare_9 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto9.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_9);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue9);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();

			c.next();

			nameColumn = c.getColumnIndex(People._ID);
			name = c.getString(nameColumn);
			idvalue10 = name;
			id_eliminare_10 = People.CONTENT_URI.parse(People.CONTENT_URI
					.getPath()
					+ "/" + name);
			nameColumn = c.getColumnIndex(People.NAME);
			name = c.getString(nameColumn);
			contatto10.setText(name);
			cmd1 = (ImageButton) findViewById(R.id.cmd_submit_elimina_10);
			cmd1.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					showAlert(
							"Cancellazione Contato",
							R.drawable.icon,
							"Si desidera cancellare il contatto dalla tua rubrica?\n",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									getCancella(idvalue10);
									all();
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									all();
								}
							}, false, null);
				}
			});

			c.commitUpdates();

			break;

		}

	}

	private void creazioneAgenda1() {
		// 4 elementi
		createPerson("Bello Eleonora", "Android company",
				"Prova di inserimento 3", "0825801077", null,
				"eleonora@gmail.com", "Ariano Irpino Avellino");
		createPerson("Rossi Mario", "Android company",
				"Prova di inserimento 4", "082577547", null,
				"mariorossi.paone@gmail.com", "Ariano Irpino Avellino");
		createPerson("Paone Maria Rosaria", "Android company",
				"Prova di inserimento 1", "3287777", null,
				"mariarosaria.paone@gmail.com", "Ariano Irpino Avellino");
		createPerson("Grasso Marco", "Android company",
				"Prova di inserimento 2", "3289168597", null,
				"grasso.marco@gmail.com", "Ariano Irpino Avellino");

	}

	private void creazioneAgenda2() {
		// 5 elementi
		createPerson("Gerardo Canfora", "Android company",
				"Prova di inserimento 5", "8254586", null, "gerardo@gmail.com",
				"Benevento");
		createPerson("Antonio Cuomo", "Android company",
				"Prova di inserimento 6", "159588", null, "antonio@gmail.com",
				"Benevento");
		createPerson("Francesco Pacilio", "Android company",
				"Prova di inserimento 7", "082497856", null,
				"francesco@gmail.com", "Benevento");
		createPerson("Raffaele Mezzullo", "Android company",
				"Prova di inserimento 8", "", null, "raffaele@gmail.com",
				"Benevento");
		createPerson("Eugenio", "Android company", "Prova di inserimento 9",
				"", null, "eugenio@gmail.com", "Benevento");

	}

	private boolean controlla() {
		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		boolean result = super.onCreateOptionsMenu(menu);
		menu.add(0, 0, "Contatti", R.drawable.icon);
		menu.add(0, 1, "Ricerca Contatto", R.drawable.person);
		menu.add(0, 2, "Inserisci Contatto", R.drawable.plus);
		menu.add(0, 3, "Cancella Contatti", R.drawable.del);

		return result;
	}

	@Override
	public boolean onOptionsItemSelected(Item item) {
		switch (item.getId()) {
		case RICERCA:
			ricerca();
			break;
		case INSERIMENTO:
			inserimento();
			break;
		case CONTATTI:
			all();

			break;
		case DELETE:
			showAlert(
					"Cancellazione Rubrica",
					R.drawable.icon,
					"Si desidera cancellare tutti i contatti presenti in rubrica.\nVuoi effettuare la cancellazione della tua rubrica?",
					"Si", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface arg1, int arg0) {
							delete();

						}
					}, "No", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface arg1, int arg0) {

						}
					}, false, null);

			break;
		}
		return super.onOptionsItemSelected(item);
	}

	// Ricerca contatto
	private void ricerca() {
		setContentView(R.layout.reserch);
		EditText edit_input = (EditText) findViewById(R.id.edit_input);
		Button cmd = (Button) findViewById(R.id.cmd_submit);
		cmd.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				EditText edit_input = (EditText) findViewById(R.id.edit_input);
				query = edit_input.getText().toString();
				String clause = android.provider.Contacts.People.NAME
						+ " LIKE '%" + query + "%'";
				final Cursor c = managedQuery(
						android.provider.Contacts.People.CONTENT_URI, null, // Which
						clause, android.provider.Contacts.People.NAME + " ASC");

				if (!c.first()) {

					showAlert(
							"Ricerca P-Grid",
							R.drawable.icon,
							"Utente non trovato in rubrica locale.\nVuoi effettuare la ricerca con P-Grid?",
							"Si", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									inserimento(query);
									
								}
							}, "No", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									ricerca();

								}
							}, false, null);

				}
				getColumnData(c);
				

			}
		});

	}

	private void inserimento(String nome) {

		// LINK CON PGRID4

		String risultato = pGridManager.search(nome);
		setContentView(R.layout.ins);

		final EditText edit_input = (EditText) findViewById(R.id.edit_input);
		final EditText edit_cell_home = (EditText) findViewById(R.id.edit_cell_home);
		final EditText edit_cell_mobile = (EditText) findViewById(R.id.edit_cell_mobile);
		final EditText e_mail = (EditText) findViewById(R.id.e_mail);
		final EditText a_ddress = (EditText) findViewById(R.id.address);
		if (nome != null && risultato != null) {
			Log.v("MyMessage", "Trovato contatto " + risultato);
			
			StringTokenizer st = new StringTokenizer(risultato, "_|_");
			
			edit_input.setText(st.nextToken());
			edit_cell_home.setText(st.nextToken());
			edit_cell_mobile.setText(st.nextToken());
			e_mail.setText(st.nextToken());
			a_ddress.setText(st.nextToken());
			
			Button cmd = (Button) findViewById(R.id.cmd_submit);

			cmd.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View arg0) {

					String nome = edit_input.getText().toString();
					String numero_home = edit_cell_home.getText().toString();
					String numero_mobile = edit_cell_mobile.getText()
							.toString();
					String email = e_mail.getText().toString();
					String address = a_ddress.getText().toString();

					if ((nome.equals(null) || (numero_home.equals(null) && numero_mobile
							.equals(null)))
							|| (nome.equals("") || (numero_home.equals("") && numero_mobile
									.equals("")))) {

						showAlert(
								"Errore inserimento",
								R.drawable.del,
								"Inserimento non effettuato.\n"
										+ "Si prega di inserire il nome ed il numero",
								"Close", new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface arg1,
											int arg0) {
										inserimento();

									}
								}, null, null, false, null);

					} else {

						Uri p = createPerson(nome);

						setNumberMobile(p, numero_mobile);
						setNumberHome(p, numero_home);
						setEmail(p, email);
						setAddress(p, address);
						String path = "";
						String idPeope = "";
						String share = "";

						CheckBox checkBox = (CheckBox) findViewById(R.id.check2);
						if (checkBox.isChecked()) {
							// INSERIRE IN DB SHARE
							path = p.getPath();
							idPeope = p.getPath().substring(
									p.getPath().lastIndexOf("/") + 1);
							share = nome + "_|_" + numero_home + "_|_"
									+ numero_mobile + "_|_" + email + "_|_"
									+ address;
							try {
								db = db_Context.openDatabase(DATABASE_NAME,
										null);
							} catch (FileNotFoundException e) {
								try {
									db = db_Context.createDatabase(
											DATABASE_NAME, DATABASE_VERSION, 0,
											null);
									db = db_Context.openDatabase(DATABASE_NAME,
											null);
								} catch (FileNotFoundException e1) {
									db = null;
								}
							}

							db.execSQL(DATABASE_CREATE);
							db.execSQL("INSERT INTO " + DATABASE_TABLE
									+ " (id_utente, dati)" + " VALUES ('"
									+ idPeope + "', '" + share + "');");

							Cursor co = db.query(DATABASE_TABLE, new String[] {
									"id_utente", "dati" }, null, null, null,
									null, null);

							Log.v("MyMessage", "Inserimento avviato");
							pGridManager.insert(share);
						}
						/*
						 * if (controlla()) {
						 * 
						 * showAlert("Inserimento", R.drawable.done,
						 * "Inserimento effettuato.\n" + "", "Close", new
						 * DialogInterface.OnClickListener() {
						 * 
						 * @Override public void onClick(DialogInterface arg1,
						 * int arg0) { Ricerca();
						 * 
						 * } }, null, null, false, null);
						 * 
						 * } else { showAlert("Inserimento",
						 * "Inserimento non effettuato.\n" +
						 * "Contattare il produttore", "Close", null, null,
						 * null, false, null);
						 * 
						 * }
						 */

						showAlert("Inserimento", R.drawable.done,
								"Inserimento effettuato.\n" + "", "Close",
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface arg1,
											int arg0) {
										ricerca();

									}
								}, null, null, false, null);

					}
				}
			});

		} else {
			showAlert("Ricerca Fallita", R.drawable.icon, "Contatto non trovato\n", "Close", null, null, null, false, null);
		}
	}

	// Inserimento nuovo contatto
	private void inserimento() {
		setContentView(R.layout.ins);
		Button cmd = (Button) findViewById(R.id.cmd_submit);

		cmd.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				EditText edit_input = (EditText) findViewById(R.id.edit_input);
				EditText edit_cell_home = (EditText) findViewById(R.id.edit_cell_home);
				EditText edit_cell_mobile = (EditText) findViewById(R.id.edit_cell_mobile);
				EditText e_mail = (EditText) findViewById(R.id.e_mail);
				EditText a_ddress = (EditText) findViewById(R.id.address);

				String nome = edit_input.getText().toString();
				String numero_home = edit_cell_home.getText().toString();
				String numero_mobile = edit_cell_mobile.getText().toString();
				String email = e_mail.getText().toString();
				String address = a_ddress.getText().toString();

				if ((nome.equals(null) || (numero_home.equals(null) && numero_mobile
						.equals(null)))
						|| (nome.equals("") || (numero_home.equals("") && numero_mobile
								.equals("")))) {

					showAlert(
							"Errore inserimento",
							R.drawable.del,
							"Inserimento non effettuato.\n"
									+ "Si prega di inserire il nome ed il numero",
							"Close", new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									inserimento();

								}
							}, null, null, false, null);

				} else {

					Uri p = createPerson(nome);

					setNumberMobile(p, numero_mobile);
					setNumberHome(p, numero_home);
					setEmail(p, email);
					setAddress(p, address);
					String path = "";
					String idPeope = "";
					String share = "";

					CheckBox checkBox = (CheckBox) findViewById(R.id.check2);
					if (checkBox.isChecked()) {
						// INSERIRE IN DB SHARE
						path = p.getPath();
						idPeope = p.getPath().substring(
								p.getPath().lastIndexOf("/") + 1);
						share = nome + "_|_" + numero_home + "_|_"
								+ numero_mobile + "_|_" + email + "_|_"
								+ address;
						try {
							db = db_Context.openDatabase(DATABASE_NAME, null);
						} catch (FileNotFoundException e) {
							try {
								db = db_Context.createDatabase(DATABASE_NAME,
										DATABASE_VERSION, 0, null);
								db = db_Context.openDatabase(DATABASE_NAME,
										null);
							} catch (FileNotFoundException e1) {
								db = null;
							}
						}

						db.execSQL(DATABASE_CREATE);
						db.execSQL("INSERT INTO " + DATABASE_TABLE
								+ " (id_utente, dati)" + " VALUES ('" + idPeope
								+ "', '" + share + "');");

						Cursor co = db.query(DATABASE_TABLE, new String[] {
								"id_utente", "dati" }, null, null, null, null,
								null);

						Log.v("MyMessage", "Inserimento avviato");
						pGridManager.insert(share);

					}

					showAlert("Inserimento", R.drawable.done,
							"Inserimento effettuato.\n" + "", "Close",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									ricerca();

								}
							}, null, null, false, null);

				}
			}
		});

	}

	private Uri setNumberMobile(Uri person, String numero) {
		ContentValues number = new ContentValues();
		List<String> pathList = person.getPathSegments();
		String pathLeaf = pathList.get(pathList.size() - 1);
		Uri phoneUpdate = null;
		if (numero != null) {
			number.put(Contacts.Phones.PERSON_ID, pathLeaf);
			number.put(Contacts.Phones.NUMBER, numero);
			number.put(Contacts.Phones.TYPE, Contacts.Phones.MOBILE_TYPE);
			phoneUpdate = getContentResolver().insert(
					Contacts.Phones.CONTENT_URI, number);
			if (phoneUpdate == null) {
			}

		}
		return phoneUpdate;
	}

	private Uri setNumberHome(Uri person, String numero) {
		ContentValues number = new ContentValues();
		List<String> pathList = person.getPathSegments();
		String pathLeaf = pathList.get(pathList.size() - 1);
		Uri phoneUpdate = null;
		if (numero != null) {
			number.put(Contacts.Phones.PERSON_ID, pathLeaf);
			number.put(Contacts.Phones.NUMBER, numero);
			number.put(Contacts.Phones.TYPE, Contacts.Phones.HOME_TYPE);
			phoneUpdate = getContentResolver().insert(
					Contacts.Phones.CONTENT_URI, number);
			if (phoneUpdate == null) {
				// throw new Exception("Failed to insert mobile phone number");
			}

		}
		return phoneUpdate;
	}

	private void setEmail(Uri person, String newemail) {
		List<String> pathList = person.getPathSegments();
		String pathLeaf = pathList.get(pathList.size() - 1);

		ContentValues email = new ContentValues();
		if (newemail != null) {
			email.put(Contacts.ContactMethods.PERSON_ID, pathLeaf);
			email.put(Contacts.ContactMethods.KIND,
					Contacts.ContactMethods.EMAIL_KIND);
			email.put(Contacts.ContactMethods.DATA, newemail);
			email.put(Contacts.ContactMethods.TYPE,
					Contacts.ContactMethods.EMAIL_KIND_PRIMARY_TYPE);
			Uri emailUpdate = getContentResolver().insert(
					Uri.withAppendedPath(person,
							Contacts.ContactMethods.CONTENT_URI.getPath()
									.substring(1)), email);
			String ok = emailUpdate.getHost();// Conctact_metos/id
			String okk = emailUpdate.getPath();
			String we = okk + " - ";
			String ow = we;

			if (emailUpdate == null) {

			}
		}
	}

	private void setNotes(Uri person, String notes) {
		List<String> pathList = person.getPathSegments();
		String pathLeaf = pathList.get(pathList.size() - 1);
		ContentValues v = new ContentValues();
		if (notes != null) {
			v.put(Contacts.ContactMethods.PERSON_ID, pathLeaf);
			v.put(Contacts.PeopleColumns.NOTES, notes);
			Uri notesUpdate = getContentResolver().insert(
					Uri.withAppendedPath(person,
							Contacts.ContactMethods.CONTENT_URI.getPath()
									.substring(1)), v);
			if (notesUpdate == null) {
				// throw new Exception("Failed to insert primary email");
			}
		}
	}

	private Uri createPerson(String name) {
		ContentValues values = new ContentValues();
		if (name != null) {
			values.put(Contacts.People.NAME, name);

			return getContentResolver().insert(Contacts.People.CONTENT_URI,
					values);
		}
		return null;
	}

	private void setAddress(Uri person, String newaddress) {
		ContentValues address = new ContentValues();
		List<String> pathList = person.getPathSegments();
		String pathLeaf = pathList.get(pathList.size() - 1);

		if (newaddress != null) {
			address.put(Contacts.ContactMethods.PERSON_ID, pathLeaf);
			address.put(Contacts.ContactMethods.KIND,
					Contacts.ContactMethods.POSTAL_KIND);
			address.put(Contacts.ContactMethods.DATA, newaddress);
			address.put(Contacts.ContactMethods.TYPE,
					Contacts.ContactMethods.POSTAL_KIND_POSTAL_TYPE);
			Uri addressUpdate = getContentResolver().insert(
					Uri.withAppendedPath(person,
							Contacts.ContactMethods.CONTENT_URI.getPath()
									.substring(1)), address);
			if (addressUpdate == null) {
				// throw new Exception("Failed to insert primary email");
			}
		}
	}

	private void setFax(Uri newPerson, String fax) {
		ContentValues number = new ContentValues();
		List<String> pathList = newPerson.getPathSegments();
		String pathLeaf = pathList.get(pathList.size() - 1);
		Uri phoneUpdate;
		// add fax number
		if (fax != null) {
			number = new ContentValues();
			number.put(Contacts.Phones.PERSON_ID, pathLeaf);
			number.put(Contacts.Phones.NUMBER, fax);
			number.put(Contacts.Phones.TYPE, Contacts.Phones.WORK_FAX_TYPE);
			phoneUpdate = getContentResolver().insert(
					Contacts.Phones.CONTENT_URI, number);
		}
	}

	private void createPerson(String personName, String nameCompany,
			String notes, String numero, String fax, String newemail,
			String newaddress) {
		ContentValues values = new ContentValues();
		if (personName != null)
			values.put(Contacts.People.NAME, personName);
		if (nameCompany != null)
			values.put(Contacts.People.COMPANY, nameCompany);
		if (notes != null)
			values.put(Contacts.People.NOTES, notes);

		Uri newPerson = getContentResolver().insert(
				Contacts.People.CONTENT_URI, values);
		if (newPerson != null) {
			List<String> pathList = newPerson.getPathSegments();
			String pathLeaf = pathList.get(pathList.size() - 1);
			ContentValues number = new ContentValues();
			Uri phoneUpdate;

			// add mobile phone number
			if (numero != null) {

				number.put(Contacts.Phones.PERSON_ID, pathLeaf);
				number.put(Contacts.Phones.NUMBER, numero);
				number.put(Contacts.Phones.TYPE, Contacts.Phones.MOBILE_TYPE);
				phoneUpdate = getContentResolver().insert(
						Contacts.Phones.CONTENT_URI, number);
				if (phoneUpdate == null) {
				}
			}
			// add fax number
			if (fax != null) {
				number = new ContentValues();
				number.put(Contacts.Phones.PERSON_ID, pathLeaf);
				number.put(Contacts.Phones.NUMBER, fax);
				number.put(Contacts.Phones.TYPE, Contacts.Phones.WORK_FAX_TYPE);
				phoneUpdate = getContentResolver().insert(
						Contacts.Phones.CONTENT_URI, number);
			}
			// add email

			ContentValues email = new ContentValues();
			if (newemail != null) {
				email.put(Contacts.ContactMethods.PERSON_ID, pathLeaf);
				email.put(Contacts.ContactMethods.KIND,
						Contacts.ContactMethods.EMAIL_KIND);
				email.put(Contacts.ContactMethods.DATA, newemail);
				email.put(Contacts.ContactMethods.TYPE,
						Contacts.ContactMethods.EMAIL_KIND_PRIMARY_TYPE);
				Uri emailUpdate = getContentResolver().insert(
						Uri.withAppendedPath(newPerson,
								Contacts.ContactMethods.CONTENT_URI.getPath()
										.substring(1)), email);
				if (emailUpdate == null) {
				}
			}
			// add postal address

			ContentValues address = new ContentValues();

			if (newaddress != null) {
				address.put(Contacts.ContactMethods.PERSON_ID, pathLeaf);
				address.put(Contacts.ContactMethods.KIND,
						Contacts.ContactMethods.POSTAL_KIND);
				address.put(Contacts.ContactMethods.DATA, newaddress);
				address.put(Contacts.ContactMethods.TYPE,
						Contacts.ContactMethods.POSTAL_KIND_POSTAL_TYPE);
				Uri addressUpdate = getContentResolver().insert(
						Uri.withAppendedPath(newPerson,
								Contacts.ContactMethods.CONTENT_URI.getPath()
										.substring(1)), address);
				if (addressUpdate == null) {
				}
			}
		}
	}

	private void getColumnData(Cursor cur) {
		setContentView(R.layout.ricerca);

		try {

			int nameColumn = cur.getColumnIndex(People.NAME);
			id = cur.getColumnIndex(People._ID);

			EditText name_1 = (EditText) findViewById(R.id.name_1);
			EditText name_2 = (EditText) findViewById(R.id.name_2);
			EditText name_3 = (EditText) findViewById(R.id.name_3);

			Button cmd_name_1 = (Button) findViewById(R.id.cmd_submit_name1);
			Button cmd_name_2 = (Button) findViewById(R.id.cmd_submit_name2);
			Button cmd_name_3 = (Button) findViewById(R.id.cmd_submit_name3);

			switch (cur.count()) {

			case 1:

				name = cur.getString(nameColumn);
				idvalue = cur.getString(id);

				name_1.setText(name);

				cmd_name_1.setCursorVisible(true);
				cmd_name_1.setOnClickListener(new Button.OnClickListener() {
					@Override
					public void onClick(View arg0) {
						getRicerca(idvalue);

					}
				});
				break;

			case 2:

				name = cur.getString(nameColumn);
				idvalue = cur.getString(id);
				idvalue1 = idvalue;
				name_1.setText(name);

				cmd_name_1.setCursorVisible(true);
				cmd_name_1.setOnClickListener(new Button.OnClickListener() {
					@Override
					public void onClick(View arg0) {
						getRicerca(idvalue1);

					}
				});

				cur.next();

				name = cur.getString(nameColumn);
				idvalue = cur.getString(id);
				idvalue2 = idvalue;
				name_2.setText(name);

				cmd_name_2.setCursorVisible(true);
				cmd_name_2.setOnClickListener(new Button.OnClickListener() {
					@Override
					public void onClick(View arg0) {
						getRicerca(idvalue2);

					}
				});

				break;

			default:
				name = cur.getString(nameColumn);
				idvalue = cur.getString(id);
				idvalue1 = idvalue;
				name_1.setText(name);

				cmd_name_1.setCursorVisible(true);
				cmd_name_1.setOnClickListener(new Button.OnClickListener() {
					@Override
					public void onClick(View arg0) {
						getRicerca(idvalue1);

					}
				});

				cur.next();

				name = cur.getString(nameColumn);
				idvalue = cur.getString(id);
				idvalue2 = idvalue;
				name_2.setText(name);

				cmd_name_2.setCursorVisible(true);
				cmd_name_2.setOnClickListener(new Button.OnClickListener() {
					@Override
					public void onClick(View arg0) {
						getRicerca(idvalue2);

					}
				});

				cur.next();

				name = cur.getString(nameColumn);
				idvalue = cur.getString(id);
				idvalue3 = idvalue;
				name_3.setText(name);

				cmd_name_3.setCursorVisible(true);
				cmd_name_3.setOnClickListener(new Button.OnClickListener() {
					@Override
					public void onClick(View arg0) {
						getRicerca(idvalue3);

					}
				});

				break;

			}

		} catch (Exception e) {
			/*
			 * showAlert( "Ricerca", "Questa ricerca non ha prodotto nessun
			 * risultato\n" + "Il contatto non è presente", "Close", null, null,
			 * null, false, null);
			 */
		}
	}

	private void getRicerca(String id) {
		setContentView(R.layout.ins);

		EditText name_1 = (EditText) findViewById(R.id.edit_input);
		EditText cell_mobile = (EditText) findViewById(R.id.edit_cell_mobile);
		EditText cell_home = (EditText) findViewById(R.id.edit_cell_home);
		EditText email = (EditText) findViewById(R.id.e_mail);
		EditText address = (EditText) findViewById(R.id.address);
		
		String indirizzo = null;
		String posta = null;
		String numero_1 = null;
		String numero_2 = null;
		

		// Ricerca tutti i contatti
		Cursor cursorName = managedQuery(
				android.provider.Contacts.People.CONTENT_URI, null, null, null);

		boolean findPers = false;
		int mid = 0;
		while (cursorName.next()) {

			mid = cursorName.getColumnIndex(People._ID);
			if (id.equals(cursorName.getString(mid))) {
				findPers = true;
				break;
			}

		}
		String nome=null;
		if (findPers == true) {
			nome = cursorName.getString(cursorName
					.getColumnIndex(People.NAME));
			name_1.setText(nome);
		}
		// Query per il recupero di tutti i numeri telefonici
		Uri.Builder builder = People.CONTENT_URI.buildUpon();
		ContentUris.appendId(builder, cursorName.getLong(mid));
		builder.appendEncodedPath(People.Phones.CONTENT_DIRECTORY);
		Uri phoneNumbersUri = builder.build();
		Cursor phonesForPerson = managedQuery(phoneNumbersUri, new String[] {
				People.Phones._ID, People.Phones.NUMBER, People.Phones.TYPE },
				null, null, null);

		if (phonesForPerson.first()) {

			do {

				int typeOfPhone = phonesForPerson

				.getInt(phonesForPerson

				.getColumnIndex(People.Phones.TYPE));

				switch (typeOfPhone) {

				case People.Phones.MOBILE_TYPE:
					numero_1 = phonesForPerson
							.getString(phonesForPerson
							.getColumnIndex(People.Phones.NUMBER));
					cell_mobile.setText(numero_1);

					break;
				case People.Phones.HOME_TYPE:
					numero_2 = phonesForPerson

					.getString(phonesForPerson

					.getColumnIndex(People.Phones.NUMBER));
					
					cell_home.setText(numero_2);

					break;

				}

			} while (phonesForPerson.next());

		}
		// Recupera l'email del contatto
		String selection = Contacts.ContactMethods.KIND + "="
				+ Contacts.ContactMethods.EMAIL_KIND;

		Cursor emailsPerson = managedQuery(

		Contacts.ContactMethods.CONTENT_URI, null, selection,

		null, null);

		if (emailsPerson.first()) {

			do {

				int iPersonID = emailsPerson

				.getColumnIndex(Contacts.ContactMethods.PERSON_ID);

				String PersonID = emailsPerson.getString(iPersonID);

				if (PersonID.equals(id)) {

					int iTypeEmail = emailsPerson

					.getColumnIndex(Contacts.ContactMethods.TYPE);

					String TypeEmail = emailsPerson

					.getString(iTypeEmail);

					int iMail = emailsPerson

					.getColumnIndex(Contacts.ContactMethods.DATA);

					;

					switch (Integer.parseInt(TypeEmail)) {

					case Contacts.ContactMethods.EMAIL_KIND_PRIMARY_TYPE:
						posta = emailsPerson
						.getString(iMail);
						email.setText(posta);

						break;

					case Contacts.ContactMethods.EMAIL_KIND_WORK_TYPE:
						posta = emailsPerson
						.getString(iMail);
						
						email.setText(posta);

						break;

					}

				}

			} while (emailsPerson.next());

		}

		// Recupera adrees del contatto
		Cursor addressPerson = managedQuery(Contacts.ContactMethods.CONTENT_URI, null, null,null, null);

		if (addressPerson.first()) {
			Button OK;
			do {

				int iPersonID = addressPerson

				.getColumnIndex(Contacts.ContactMethods.PERSON_ID);

				String PersonID = addressPerson.getString(iPersonID);

				if (PersonID.equals(id)) {

					int iMail = addressPerson
					.getColumnIndex(Contacts.ContactMethods.DATA);
					
					indirizzo = addressPerson.getString(iMail);
					
					address.setText(indirizzo);

				}

			} while (addressPerson.next());
			

		}
		
		final String name_= nome;
		final String number_1 = null;
		final String number_2 = null;
		final String emails = posta;
		final String address_=indirizzo;
 
		Button cmd = (Button) findViewById(R.id.cmd_submit);
		final CheckBox checkBox = (CheckBox) findViewById(R.id.check2);

		cmd.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View arg0) {		
				if (checkBox.isChecked()) {
						// INSERIRE IN DB SHARE
			String path = "";
			String idPeope = "";
			String share = "";
			Uri p = createPerson(name_);
			

						path = p.getPath();
						idPeope = p.getPath().substring(
								p.getPath().lastIndexOf("/") + 1);
						share = name_ + "_|_" + number_1 + "_|_"
								+ number_2 + "_|_" + emails + "_|_"
								+ address_;
						try {
							db = db_Context.openDatabase(DATABASE_NAME, null);
						} catch (FileNotFoundException e) {
							try {
								db = db_Context.createDatabase(DATABASE_NAME,
										DATABASE_VERSION, 0, null);
								db = db_Context.openDatabase(DATABASE_NAME,
										null);
							} catch (FileNotFoundException e1) {
								db = null;
							}
						}

						db.execSQL(DATABASE_CREATE);
						db.execSQL("INSERT INTO " + DATABASE_TABLE
								+ " (id_utente, dati)" + " VALUES ('" + idPeope
								+ "', '" + share + "');");

						Cursor co = db.query(DATABASE_TABLE, new String[] {
								"id_utente", "dati" }, null, null, null, null,
								null);

						Log.v("MyMessage", "Inserimento avviato");
						pGridManager.insert(share);

					}
			
					showAlert("Inserimento", R.drawable.done,
							"Inserimento effettuato.\n" + "", "Close",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface arg1,
										int arg0) {
									ricerca();

								}
							}, null, null, false, null);

				}
		}); //Chiude bottone

		
		
		
		
		

	}//chiude il metodo getRicerca
}